/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

import com.xuexiang.xui_lib.util.AttrValue;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一标题栏
 * <p>
 * 设置中间标题，中间副标题，右边标题，左边标题
 * 设置返回图标，右边图标(可选)
 * 设置TitleBar的Element，设置字体颜色
 * 设置字体大小，左右间距，左边标题间距
 *
 * @author jiangjianming
 * @since 2021-04-13
 */
public class TitleBarLayout extends DependentLayout {

    private Context mContext;

    // 主标题属性
    private int mTitleSize;
    private String mTitleText;
    private Color mTitleColor;
    private String mSubtitleText;
    private int mSubtitleSize;
    private Color mSubtitleColor;

    private int mLeftSubtitleSize;
    private String mLeftSubtitle;
    private Color mLeftSubtitleColor;

    private int mRightSubtitleSize;
    private String mRightSubtitle;
    private Color mRightSubtitleColor;

    private int mLeftImageMediaRes;
    private int mRightImageMediaRes;

//    private int mImageSize;
    private int mRootMargin;
//    private int mSubtitlePadding;
    private DirectionalLayout mTitleContainer;
    private DirectionalLayout mRightContainer;
    private DirectionalLayout mLeftContainer;

    private ITitleBarClickListener mClickListener;
    private Element mLeftImageElement;
    private Element mRightImageElement;

    private Map<String, Component> mComponents = new HashMap<>();

    /**
     * 点击事件类型
     *
     * @since 2021-04-13
     */
    public enum ClickEventType {
        /**
         * 左边ICON点击事件
         */
        LEFT_ICON_CLICK,
        /**
         * 右边ICON点击事件
         */
        RIGHT_ICON_CLICK
    }

    /**
     * 点击监听
     *
     * @since 2021-04-13
     */
    public interface ITitleBarClickListener {
        /**
         * 点击事件
         *
         * @param component 点击对象
         * @param type 点击类型
         */
        void onClick(Component component, ClickEventType type);
    }

    /**
     * 统一标题栏
     *
     * @param context 上下文
     */
   /* public TitleBarLayout(Context context) {
        this(context, null);
    }*/

    /**
     * 统一标题栏
     *
     * @param context 上下文
     * @param attrSet 参数
     */
    public TitleBarLayout(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * 统一标题栏
     *
     * @param context 上下文
     * @param attrSet 参数
     * @param styleName 参数
     */
    public TitleBarLayout(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        mContext = context;
        initAttrSet(context, attrSet);
        initBarChildComponent();
    }

    private void initAttrSet(Context context, AttrSet attrSet) {
        // 主标题
        mTitleText = AttrValue.get(attrSet, "title", "");
        mTitleSize = AttrValue.get(attrSet, "title_size", AttrHelper.fp2px(18, mContext));
        mTitleColor = AttrValue.get(attrSet, "title_color", Color.BLACK);
        mSubtitleText = AttrValue.get(attrSet, "sub_title", "");
        mSubtitleSize = AttrValue.get(attrSet, "sub_title_size", AttrHelper.fp2px(12, mContext));
        mSubtitleColor = AttrValue.get(attrSet, "sub_title_color", Color.BLACK);
        mLeftSubtitle = AttrValue.get(attrSet, "sub_left_title", "");
        mLeftSubtitleSize = AttrValue.get(attrSet, "sub_left_title_size", AttrHelper.fp2px(14, mContext));
        mLeftSubtitleColor = AttrValue.get(attrSet, "sub_left_title_color", Color.BLACK);
        mRightSubtitle = AttrValue.get(attrSet, "sub_right_title", "");
        mRightSubtitleSize = AttrValue.get(attrSet, "sub_right_title_size", AttrHelper.fp2px(14, mContext));
        mRightSubtitleColor = AttrValue.get(attrSet, "sub_left_title_color", Color.BLACK);
        mLeftImageElement = AttrValue.getElement(attrSet, "left_icon");
        mRightImageElement = AttrValue.getElement(attrSet, "right_icon");

        //mImageSize = AttrHelper.vp2px(24, mContext);
        mRootMargin = AttrHelper.vp2px(8, mContext);
        //mSubtitlePadding = AttrHelper.vp2px(6, mContext);
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        this.mTitleText = title;
        Text titleText = getTitleText();
        titleText.setText(title);
    }

    /**
     * 设置标题
     *
     * @param title 副标题
     */
    public void setSubTitle(String title) {
        this.mSubtitleText = title;
        Text subTitleText = getSubTitleText();
        subTitleText.setText(title);
    }

    /**
     * 设置标题相关事件
     *
     * @param clickListener 点击事件
     */
    public void setTitleClickListener(ITitleBarClickListener clickListener) {
        this.mClickListener = clickListener;
    }

    private void initBarChildComponent() {
        mTitleContainer = createAndAddTitleContainer();
        mRightContainer = createAndAddRightContainer();
        mLeftContainer = createAndAddLeftContainer();

        if (!isEmpty(mTitleText)) {
            Text titleText = getTitleText();
            titleText.setText(mTitleText);
        }
        if (!isEmpty(mSubtitleText)) {
            Text text = getSubTitleText();
            text.setText(mSubtitleText);
        }
        if (mLeftImageMediaRes > 0) {
            Image leftImage = createImage(mLeftContainer);
            leftImage.setPixelMap(mLeftImageMediaRes);
            leftImage.setClickedListener(component -> {
                mClickListener.onClick(component, ClickEventType.LEFT_ICON_CLICK);
            });
        } else if (mLeftImageElement != null) {
            Image leftImage = createImage(mLeftContainer);
            leftImage.setImageElement(mLeftImageElement);
            leftImage.setClickedListener(component -> {
                mClickListener.onClick(component, ClickEventType.LEFT_ICON_CLICK);
            });
        }
        if (!isEmpty(mLeftSubtitle)) {
            Text text = createText(mLeftContainer);
            text.setText(mLeftSubtitle);
            text.setTextSize(mLeftSubtitleSize);
            text.setTextColor(mLeftSubtitleColor);
        }
        if (!isEmpty(mRightSubtitle)) {
            Text text = createText(mRightContainer);
            text.setText(mRightSubtitle);
            text.setTextSize(mRightSubtitleSize);
            text.setTextColor(mRightSubtitleColor);
        }
        if (mRightImageMediaRes > 0) {
            Image rightImage = createImage(mRightContainer);
            rightImage.setPixelMap(mRightImageMediaRes);
            if (mClickListener != null) {
                rightImage.setClickedListener(component -> {
                    mClickListener.onClick(component, ClickEventType.RIGHT_ICON_CLICK);
                });
            }
        } else if (mRightImageElement != null) {
            Image rightImage = createImage(mRightContainer);
            rightImage.setImageElement(mRightImageElement);
            if (mClickListener != null) {
                rightImage.setClickedListener(component -> {
                    mClickListener.onClick(component, ClickEventType.RIGHT_ICON_CLICK);
                });
            }
        }
    }

    private DirectionalLayout createAndAddLeftContainer() {
        DirectionalLayout horLayout = createHLayout();
        horLayout.setAlignment(LayoutAlignment.VERTICAL_CENTER);
        // 在父容器的位置
        LayoutConfig withParentConfig = createDependentConfig();
        withParentConfig.addRule(LayoutConfig.ALIGN_PARENT_LEFT);
        withParentConfig.addRule(LayoutConfig.VERTICAL_CENTER);
        withParentConfig.setMarginLeft(mRootMargin);
        this.addComponent(horLayout, withParentConfig);
        return horLayout;
    }

    private DirectionalLayout createAndAddRightContainer() {
        DirectionalLayout horLayout = createHLayout();
        horLayout.setAlignment(LayoutAlignment.VERTICAL_CENTER);
        LayoutConfig witchParentConfig = createDependentConfig();
        witchParentConfig.addRule(LayoutConfig.ALIGN_PARENT_RIGHT);
        witchParentConfig.addRule(LayoutConfig.VERTICAL_CENTER);
        witchParentConfig.setMarginRight(mRootMargin);
        this.addComponent(horLayout, witchParentConfig);
        return horLayout;
    }

    private DirectionalLayout createAndAddTitleContainer() {
        DirectionalLayout veLayout = createVLayout();
        veLayout.setAlignment(LayoutAlignment.HORIZONTAL_CENTER);
        LayoutConfig depConfig = createDependentConfig();
        depConfig.addRule(LayoutConfig.CENTER_IN_PARENT);
        this.addComponent(veLayout, depConfig);
        return veLayout;
    }

    private Text getTitleText() {
        Component component = mComponents.get("title");
        if (null != component) {
            return (Text) component;
        }
        Text titleText = createText(mTitleContainer);
        titleText.setTextSize(mTitleSize);
        titleText.setTextColor(mTitleColor);
        mComponents.put("title", titleText);
        return titleText;
    }

    private Text getSubTitleText() {
        Component component = mComponents.get("subtitle");
        if (null != component) {
            return (Text) component;
        }
        Text subtitle = createText(mTitleContainer);
        subtitle.setTextSize(mSubtitleSize);
        subtitle.setTextColor(mSubtitleColor);
        mComponents.put("subtitle", subtitle);
        return subtitle;
    }

    private Text createText(DirectionalLayout container) {
        Text text = new Text(mContext);
        container.addComponent(text, createWWLayoutConfig(-1));
        return text;
    }

    private Image createImage(DirectionalLayout container) {
        Image image = new Image(mContext);
        int padding = AttrHelper.vp2px(6, mContext);
        image.setPadding(padding, padding, padding, padding);
        image.setScaleMode(Image.ScaleMode.STRETCH);
        container.addComponent(image, createLayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT, LayoutAlignment.VERTICAL_CENTER));
        return image;
    }

    /**
     * 创建水平布局容器
     *
     * @return return
     */
    private DirectionalLayout createHLayout() {
        DirectionalLayout direct = new DirectionalLayout(mContext);
        DirectionalLayout.LayoutConfig layoutConfig = createWWLayoutConfig(DirectionalLayout.VERTICAL);
        direct.setLayoutConfig(layoutConfig);
        direct.setOrientation(DirectionalLayout.HORIZONTAL);
        return direct;
    }

    /**
     * 创建添加到当前容器的Config
     *
     * @return return
     */
    private LayoutConfig createDependentConfig() {
        return new LayoutConfig(
            ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
    }

    /**
     * 创建垂直布局容器
     *
     * @return return
     */
    private DirectionalLayout createVLayout() {
        DirectionalLayout direct = new DirectionalLayout(mContext);
        DirectionalLayout.LayoutConfig layoutConfig = createWWLayoutConfig(DirectionalLayout.VERTICAL);
        direct.setLayoutConfig(layoutConfig);
        direct.setOrientation(DirectionalLayout.VERTICAL);
        return direct;
    }

    /**
     * 创建 包裹内容Config
     *
     * @param alignment alignment
     * @return return
     */
    private DirectionalLayout.LayoutConfig createWWLayoutConfig(int alignment) {
        return createLayoutConfig(DirectionalLayout.LayoutConfig.MATCH_CONTENT,
            DirectionalLayout.LayoutConfig.MATCH_CONTENT, alignment);
    }

    private DirectionalLayout.LayoutConfig createLayoutConfig(int width, int height, int alignment) {
        DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(width, height);
        config.alignment = alignment;
        return config;
    }

    private boolean isEmpty(String txt) {
        if (txt == null || txt.isEmpty()) {
            return true;
        }
        return false;
    }

}
